﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Subgurim.Controles;

namespace GlobalTracker.WebSite.AreaRestrita
{
  public partial class HistoricoNavegacao : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        WebServiceReference.GeneralService service = new WebServiceReference.GeneralService();
        service.Url = GlobalTracker.WebSite.Properties.Settings.Default.GlobalTracker_WebSite_WebServiceReference_GeneralService;

        DataSet ds;

        if (Session["TipoLogin"] == "Administrador")
          ds = service.RecuperarListagem(string.Empty, "SELECT IMEI, NOME FROM DISPOSITIVOMOVEL");
        else
          ds = service.RecuperarListagem(string.Empty, "SELECT IMEI, NOME FROM DISPOSITIVOMOVEL WHERE IMEI = '" + Session["Login"].ToString() + "'"); 

        if (ds.Tables.Count > 0)
        {
          for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
          {
            ComboBoxDispositivoMovel.Items.Add(ds.Tables[0].Rows[i]["IMEI"].ToString() + " - " + ds.Tables[0].Rows[i]["NOME"].ToString(),
              ds.Tables[0].Rows[i]["IMEI"].ToString());
          }

          if (ds.Tables[0].Rows.Count > 0)
          {
            ComboBoxDispositivoMovel.SelectedIndex = 0;

            CarregarMapa(service);
          }
        }

        service.Dispose();
        service = null;
      }
    }

    private void CarregarMapa(WebServiceReference.GeneralService service)
    {
      LimparAreaDefinida();
      DataSet dsCoordenadas = service.RecuperarUltimasCoordenadasDispositivoMovel(string.Empty, ComboBoxDispositivoMovel.SelectedItem.Value.ToString(), int.Parse(ASPxComboBoxColetas.SelectedItem.Value.ToString()));

      if (dsCoordenadas.Tables.Count > 0)
      {
        if (dsCoordenadas.Tables[0].Rows.Count == 1)
        {
          GMarker coordenada = new GMarker(new GLatLng(Convert.ToDouble(dsCoordenadas.Tables[0].Rows[0]["LATITUDE"].ToString()), Convert.ToDouble(dsCoordenadas.Tables[0].Rows[0]["LONGITUDE"].ToString())));
          GInfoWindow janela = new GInfoWindow(coordenada, "<center>" + dsCoordenadas.Tables[0].Rows[0]["DATA"].ToString() + "</center>", true);
          GMapAreaMonitoramento.setCenter(new GLatLng(Convert.ToDouble(dsCoordenadas.Tables[0].Rows[0]["LATITUDE"].ToString()), Convert.ToDouble(dsCoordenadas.Tables[0].Rows[0]["LONGITUDE"].ToString())));

          GMapAreaMonitoramento.Add(janela);
        }
        else
        {
          List<GLatLng> pontos = new List<GLatLng>();

          for (int j = 0; j < dsCoordenadas.Tables[0].Rows.Count; j++)
          {
            if (j == 0)
              GMapAreaMonitoramento.setCenter(new GLatLng(Convert.ToDouble(dsCoordenadas.Tables[0].Rows[j]["LATITUDE"].ToString()), Convert.ToDouble(dsCoordenadas.Tables[0].Rows[j]["LONGITUDE"].ToString())));

            pontos.Add(new GLatLng(Convert.ToDouble(dsCoordenadas.Tables[0].Rows[j]["LATITUDE"].ToString()), Convert.ToDouble(dsCoordenadas.Tables[0].Rows[j]["LONGITUDE"].ToString())));            
          }

          GPolyline linha = new GPolyline(pontos, "FF0000", 1);
          GMapAreaMonitoramento.Add(linha);
        }
      }
    }

    private void LimparAreaDefinida()
    {
      try
      {
        GMapAreaMonitoramento.resetPolygon();
      }
      catch { }

      try
      {
        GMapAreaMonitoramento.resetControls();
      }
      catch { }

      try
      {
        GMapAreaMonitoramento.resetPolylines();
      }
      catch { }

      try
      {
        GMapAreaMonitoramento.resetInfoWindows();
      }
      catch { }

      try
      {
        GMapAreaMonitoramento.resetPolygon();
      }
      catch { }

      Session["PontosOriginal"] = null;
    }

    protected void ASPxComboBoxColetas_SelectedIndexChanged(object sender, EventArgs e)
    {
      WebServiceReference.GeneralService service = new WebServiceReference.GeneralService();
      service.Url = GlobalTracker.WebSite.Properties.Settings.Default.GlobalTracker_WebSite_WebServiceReference_GeneralService;
      CarregarMapa(service);
      service.Dispose();
      service = null;
    }

    protected void ComboBoxDispositivoMovel_SelectedIndexChanged(object sender, EventArgs e)
    {
      WebServiceReference.GeneralService service = new WebServiceReference.GeneralService();
      service.Url = GlobalTracker.WebSite.Properties.Settings.Default.GlobalTracker_WebSite_WebServiceReference_GeneralService;
      CarregarMapa(service);
      service.Dispose();
      service = null;
    }
  }
}